﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.DataEntity;
//using System.Web.Mail;
using System.Net.Mail;    
using TimKen.App;

namespace TimKen.Web.User
{
    public partial class GetMyPassword : TimKen.App.WebPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentUserName == null || CurrentUserName == "")
                {
                    Alert("您必须登录！", "../Index.aspx");
                }
                BindResources();
            }
        }

        protected void btGetPassword_Click(object sender, EventArgs e)
        {
            UserS u = TimKen.BussLogic.UserFacade.GetUser(txtUserName.Text.ToLower().Trim());
            if (u != null)
            {
                string email, password;
                UserAccount ua = u as UserAccount;
                if (ua != null)
                {
                    email = ua.Email;
                    password = ua.Password;
                }
                else
                {
                    WebUser wu = u as WebUser;
                    email = wu.Email;
                    password = wu.Password;
                }
                if (email == "" || email == null)
                {
                    Alert(GetLangString("NoEmail"), "/User/GetMyPassword.aspx");
                    return;
                }


                try
                {
                    MailMessage mm = new MailMessage();

                    MailAddress to = new MailAddress(email);
                    mm.To.Add(to);
                    mm.Subject = GetLangString("FirstBridgePassword");
                    mm.Body = GetLangString("YourPasswordIs") + password;

                    Tools.SendEmail(mm);
                }
                catch (CstmException ex)
                {
                    Alert(ex.Message, "/User/GetMyPassword.aspx");
                    return;
                }

                Alert(GetLangString("EmailSend"), "/User/GetMyPassword.aspx");


            }
            else
            {
                Alert(GetLangString("NoUser"), "/User/GetMyPassword.aspx");
            }
        }

        protected void BindResources()
        {
            btGetPassword.Text = GetLangString("GetMyPassword");
        }
    }
}
